//
//  LoginViewController.m
//  ScoreManager
//
//  Created by 何凯楠 on 2017/4/19.
//  Copyright © 2017年 HeXiaoBa. All rights reserved.
//

#import "LoginViewController.h"
#import "XTSqliteUtils.h"
#import "HomeNavigationController.h"
#import "ViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)login:(id)sender {
    
    NSString *username = self.nameField.text;
    NSString *password = self.passwordField.text;
    if (username.length == 0 || password.length == 0) {
        [self showAlertMsg:@"请填写用户名和密码"];
        return;
    }
    
    NSDictionary *result = [XTSqliteUtils loginWithUsername:username password:password];
    BOOL flag = result[@"status"];
    if (flag) {
        NSInteger type = [result[@"type"] integerValue];
        [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] setObject:@(type) forKey:@"type"];
        
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        [UIApplication sharedApplication].keyWindow.rootViewController = [main instantiateViewControllerWithIdentifier:@"home"];
        
    } else {
        [self showAlertMsg:@"用户名或密码输入错误"];
    }
    
}

- (IBAction)registerClick:(id)sender {
    
}

- (void)showAlertMsg:(NSString *)msg {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];
}

@end
